using Gateway.Common.Enums;
using Gateway.Domain.EFCore.Configuration;
using Gateway.Domain.Entities;
using Gateway.Domain.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Newtonsoft.Json;

namespace Gateway.Domain.Configuration;

/// <summary>
/// 条件分支相关配配置
/// </summary>
public class WorkFlowDesignBranchConfiguration : EntityConfiguration<WorkFlowDesignBranch>
{
    public override void Configure(EntityTypeBuilder<WorkFlowDesignBranch> builder)
    {
        builder.ToTable(t => t.HasComment("条件分支相关配配置"));
        builder.Property(x => x.Conditions).HasMaxLength(2000)
            .HasComment("分支条件")
            .HasConversion(x => JsonConvert.SerializeObject(x),
                p => JsonConvert.DeserializeObject<List<BranchCondition>>(p));

        builder.HasOne(x => x.Design).WithOne(p => p.Branch)
            .HasForeignKey<WorkFlowDesignBranch>(x => x.WorkFlowDesignId)
            .OnDelete(DeleteBehavior.Cascade);
        base.Configure(builder);
    }
}